package com.bgthrid.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import com.andview.refreshview.XRefreshView;
import com.kldy8.kldyw.R;
import java.util.ArrayList;
import com.kldy8.kldyw.bean.New;
import com.kldy8.kldyw.find.adapter.NewInfoAdapter;
import com.kldy8.kldyw.logre.LoginActivity;
import com.kldy8.kldyw.system.ServerApi;
import com.kldy8.kldyw.system.ServerCallBack;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Request;

/**
 * Created by florentchampigny on 24/04/15.
 */
public class RecyclerViewFragment extends Fragment {

    static final boolean GRID_LAYOUT = false;
    private static final int ITEM_COUNT = 100;
    private String M_TAG;
    private ListView mListView;
    private NewInfoAdapter mAdapter;
    private XRefreshView refreshView1;
    private int page1 = 1;


    public static RecyclerViewFragment newInstance(String tag)
    {
        RecyclerViewFragment fr = new RecyclerViewFragment();
        fr.M_TAG = tag;
        return fr;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recyclerview, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mListView = (ListView) view.findViewById(R.id.listview);



        refreshView1  = (XRefreshView) view.findViewById(R.id.xrefreshview);
        refreshView1 = (XRefreshView) view.findViewById(R.id.xrefreshview);
        refreshView1.setPullLoadEnable(true);

        //设置刷新完成以后，headerview固定的时间
        refreshView1.setPinnedTime(1000);
        refreshView1.setPullLoadEnable(true);
        refreshView1.setMoveForHorizontal(true);
        refreshView1.setAutoLoadMore(true);
        refreshView1.setEmptyView(R.layout.layout_emptyview);
        refreshView1.getEmptyView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshView1.startRefresh();
            }
        });

        refreshView1.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener()
        {
            @Override
            public void onRefresh()
            {
                requestData();
            }

            @Override
            public void onLoadMore(boolean isSilence)
            {
                moreData();
            }
        });


        mAdapter = new NewInfoAdapter(getActivity(), new ArrayList<New>());
        mListView.setAdapter(mAdapter);
        initData(true,M_TAG, 1);
    }

    private void initData(final boolean isInit, String column_id, int page) {
        ServerApi.newss(column_id, ""+page, new ServerCallBack() {
            @Override
            public void onError(Request request, Exception e)
            {
                Log.e("。。。。。", e.getMessage());
                if (refreshView1!=null)
                {
                    refreshView1.stopRefresh();
                    refreshView1.stopLoadMore();
                }
            }

            @Override
            public void onResponse(String response) throws JSONException
            {
                Log.e("。。。。。", response.toString());
                Log.e("。。。。。", ""+page1);

                ArrayList<New>  mContentItems = new ArrayList<New>();


                JSONObject object = new JSONObject(response);
                if ("0".equals(object.optString("code")))
                {
                    page1++;
                    JSONArray jsonArray = object.optJSONArray("data");

                    if (isInit == true)
                    {
                        page1 = 2;
                        mContentItems = new ArrayList<New>();
                    }

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jo = jsonArray.optJSONObject(i);
                        New m_new = New.resolution(jo);
                        mContentItems.add(m_new);
                    }
                    mAdapter.replaceOrAdd(isInit, mContentItems);
                    mAdapter.notifyDataSetChanged();
                }
                else  if ("10003".equals(object.optString("code")))
                {
                    Toast.makeText(getActivity(), "暂无数据", Toast.LENGTH_LONG).show();
                }
                else  if ("10004".equals(object.optString("code")))
                {
                    Intent intent2 = new Intent();
                    intent2.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent2);
                    getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                }
                if (refreshView1!=null)
                {
                    refreshView1.stopRefresh();
                    refreshView1.stopLoadMore();
                }
            }
        });
    }

    //刷新人才求职数据
    public void requestData()
    {
        initData(true,M_TAG, 1);
    }
    //添加人才求职数据
    public void moreData()
    {
        initData(false,M_TAG,page1);
    }

}
